<?php

namespace App\System\Coupon\Repositories;

use App\Module\Api\Controllers\Request\TableRequest;
use App\System\Base\Helpers\Traits\SinglePattern;
use App\System\Base\Repositories\Repository;
use App\System\Coupon\Model\CouponUser;


class CouponUserRepository extends Repository
{
    use SinglePattern;

    /**
     * 生成购物券
     * @param array $data 数据数组
     * @return bool|mixed
     */
    public function createCoupon(array $data)
    {
        $res = CouponUser::create($data);
        if(!$res)
        {
            return false;
        }
        return $res->id;
    }


    /**
     * 获取列表
     * @param $start_time
     * @param $end_time
     * @param int $status
     * @param bool $getall
     * @return mixed
     */
    public function getList($start_time,$end_time,$status=0,$getall=false)
    {
        if($getall){
            $data = CouponUser::when($start_time,function ($query)use($start_time){
                return $query->where('created_at','>=',$start_time);
            })
                ->when($end_time,function ($query)use($end_time){
                    return $query->where('created_at','<=',$end_time.' 23:59:59');
                })
                ->where('status',$status)
                ->get();
        }else{
            $data = CouponUser::when($start_time,function ($query)use($start_time){
                return $query->where('created_at','>=',$start_time);
            })
                ->when($end_time,function ($query)use($end_time){
                    return $query->where('created_at','<=',$end_time.' 23:59:59');
                })
                ->where('status',$status)
                ->paginate();
        }
        return $data;
    }

    /**
     * API 获取购物券记录列表
     * @param TableRequest $request
     * @param $status
     * @param $user_id
     * @return mixed
     */
    public function getApiList(TableRequest $request,$status,$user_id)
    {
        $status = $status == 1 ? 0 : [1,2];
        if(!$status){
            $data = CouponUser::where("user_id",$user_id)
                ->where('status',$status)
                ->where('expire_time',">",date("Y-m-d H:i:s"))
                ->orderBy($request->orderBy,$request->orderType)
                ->paginate($request->limit,['*'],'page',$request->page);

        }
        else
        {
            $data = CouponUser::where("user_id",$user_id)
                ->whereIn('status',$status)
                ->orWhere("expire_time",'<',date("Y-m-d H:i:s"))
                ->orderBy($request->orderBy,$request->orderType)
                ->paginate($request->limit,['*'],'page',$request->page);
        }

        foreach($data as $v) {
            $item = [];
            $item['id'] = $v->id;
            $item['name'] = '新创客专享券';
            $item['cardNumber'] = '';
            $item['parValue'] = $v->amount;
            $item['endTime'] = $v->expire_time;
            $return['shoppingCards'][] = $item;
        }
        $return['total'] =$data->total();
        return $return;
    }

    /**
     * @param $id
     * @param $user_id
     * @return \Illuminate\Database\Eloquent\Model|null|static
     */
    public function getOne($id,$user_id)
    {
        return CouponUser::where('id',$id)->where("user_id",$user_id)->first();
    }

    /**
     *更新购物券信息
     */
    public function update($set,$id)
    {
        return CouponUser::where('id',$id)->update($set);
    }

    /**
     * 硬删除财务记录（请勿使用此方法）
     */
    public function deleteCoupon($user_id)
    {
        if(!$user_id)
        {
            return false;
        }
        return CouponUser::where('user_id',$user_id)->forceDelete();
    }

}